//
//  MAChildViewController.m
//  meiang_edu
//
//  Created by Liubi_Chaos_G on 2018/7/30.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAChildViewController.h"
#import "MAHomeModel.h"
#import "MAZiZhiModel.h"
#import "MAStaffModel.h"
#import "MANoImgCell.h"
#import "MAOneImgCell.h"
#import "MAThreeImgsCell.h"
#import "MAZiZhiCell.h"
#import "MAStaffCell.h"

@interface MAChildViewController ()
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

/** models */
@property(nonatomic,strong) NSMutableArray *models;

/** jigou_id */
@property(nonatomic,copy) NSString *jigou_id;

@end

@implementation MAChildViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadFuncData:) name:kMAChangeJiGouNotification object:nil];
}

- (void)reloadFuncData:(NSNotification *)noti {
    DLog(@"%@",noti.userInfo[@"jigou_id"]);
    self.jigou_id = noti.userInfo[@"jigou_id"];
    [self.myTableView.mj_header beginRefreshing];
}

- (void)setupTableView {
    
    self.myTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.myTableView.tableFooterView = [UIView new];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.myTableView.mj_header beginRefreshing];
    
    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.myTableView.mj_footer.hidden = YES;
}

- (void)loadNewData {
    
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    //    if (!userinfo.token) {
    //        [MBManager showBriefAlert:@"登录状态已过期,请重新登录"];
    //        return;
    //    }
    [MBManager showLoading];
    DLog(@"%@",self.title);
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request) {
        request.requestSerializerType = ZBJSONRequestSerializer;
        if ([self.title isEqualToString:@"动态"]) {
            request.URLString = [NSString stringWithFormat:@"%@%@?msgtype=organization_notice_activity&entity_type=机构",PICKUP_MSG_LIST,self.jigou_id];
        } else if ([self.title isEqualToString:@"留言板"]) {
            request.URLString = [NSString stringWithFormat:@"%@%@?msgtype=organization_leaveword&entity_type=机构",PICKUP_MSG_LIST,self.jigou_id];
        } else if ([self.title isEqualToString:@"资质"]) {
            request.URLString = [NSString stringWithFormat:@"%@%@?status=-1&entity_type=organization_certificate",BASE_LISTENTITY_LIST,self.jigou_id];
        } else { // 员工
            request.URLString = [NSString stringWithFormat:@"%@%@?status=1&entity_type=organization_user",BASE_LISTENTITY_LIST,self.jigou_id];
        }
        request.methodType = ZBMethodTypeGET;
        [request setValue:userinfo.token forHeaderField:@"token"];
        request.apiType = ZBRequestTypeRefresh;
    } success:^(id responseObject, apiType type) {
        [MBManager hideAlert];
        // 解析数据
        NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        DLog(@"%@",dataDict);
        NSInteger code = [dataDict[@"msgcode"] integerValue];
        if (code == 1) { // 请求成功
            [MBManager hideAlert];
            if ([self.title isEqualToString:@"动态"]) {
                self.models = [MAHomeModel mj_objectArrayWithKeyValuesArray:dataDict[@"value"]];
            } else if ([self.title isEqualToString:@"留言板"]) {
                self.models = [MAHomeModel mj_objectArrayWithKeyValuesArray:dataDict[@"value"]];
            } else if ([self.title isEqualToString:@"资质"]) {
                self.models = [MAZiZhiModel mj_objectArrayWithKeyValuesArray:dataDict[@"value"]];
            } else { // 员工
                self.models = [MAStaffModel mj_objectArrayWithKeyValuesArray:dataDict[@"value"]];
            }
            [self.myTableView reloadData];
        } else {
            [MBManager showBriefAlert:dataDict[@"message"]];
        }
    } failure:^(NSError *error) {
        DLog(@"%@",error);
        
    } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache) {
        if (isCache==YES) {
            DLog(@"使用了缓存");
        }
        [self.myTableView.mj_header endRefreshing];
        self.myTableView.mj_footer.hidden = NO;
        [self.myTableView.mj_footer endRefreshingWithNoMoreData];
    }];
}
- (void)loadMoreData {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
    if ([self.title isEqualToString:@"动态"]) {
        MAHomeModel *model = self.models[row];
        return model.cellHeight - 50.0;
    } else if ([self.title isEqualToString:@"留言板"]) {
        MAHomeModel *model = self.models[row];
        return model.cellHeight;
    } else if ([self.title isEqualToString:@"资质"]) {
        MAZiZhiModel *model = self.models[row];
        return model.cellHeight;
    } else { // 员工
        MAStaffModel *model = self.models[row];
        return model.cellHeight;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    UITableViewCell *cell;
    
    if ([self.title isEqualToString:@"动态"]) {
        MAHomeModel *model = self.models[row];
        if ([model.message_image_count integerValue] == 0) { // 无图
            MANoImgCell *noCell = [MANoImgCell cellWithTableView:tableView];
            noCell.model = model;
            noCell.headerView.hidden = YES;
            noCell.headerViewHeightCons.constant = 0;
            cell = noCell;
        } else if ([model.message_image_count integerValue] < 2) { // 1图
            
            MAOneImgCell *oneCell = [MAOneImgCell cellWithTableView:tableView];
            oneCell.model = model;
            oneCell.headerView.hidden = YES;
            oneCell.headerViewHeightCons.constant = 0;
            cell = oneCell;
        } else { // 三图
            
            MAThreeImgsCell *threeCell = [MAThreeImgsCell cellWithTableView:tableView];
            threeCell.model = model;
            threeCell.headerView.hidden = YES;
            threeCell.headerViewHeightCons.constant = 0;
            cell = threeCell;
        }
    } else if ([self.title isEqualToString:@"留言板"]) {
        MAHomeModel *model = self.models[row];
        if ([model.message_image_count integerValue] == 0) { // 无图
            MANoImgCell *noCell = [MANoImgCell cellWithTableView:tableView];
            noCell.model = model;
            cell = noCell;
        } else if ([model.message_image_count integerValue] < 2) { // 1图
            
            MAOneImgCell *oneCell = [MAOneImgCell cellWithTableView:tableView];
            oneCell.model = model;
            cell = oneCell;
        } else { // 三图
            
            MAThreeImgsCell *threeCell = [MAThreeImgsCell cellWithTableView:tableView];
            threeCell.model = model;
            cell = threeCell;
        }
    } else if ([self.title isEqualToString:@"资质"]) {
        MAZiZhiModel *model = self.models[row];
        MAZiZhiCell *zizhiCell = [MAZiZhiCell cellWithTableView:tableView];
        zizhiCell.model = model;
        cell = zizhiCell;
    } else { // 员工
        MAStaffModel *model = self.models[row];
        MAStaffCell *staffCell = [MAStaffCell cellWithTableView:tableView];
        staffCell.model = model;
        cell = staffCell;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"");
}

#pragma mark - getter & setter
- (NSMutableArray *)models {
    if (_models == nil) {
        _models = [NSMutableArray array];
    }
    return _models;
}

- (void)setJigouModel:(MAJiGouModel *)jigouModel {
    _jigouModel = jigouModel;
    self.jigou_id = jigouModel.ID;
}
@end
